package com.realmdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.facebook.stetho.Stetho;
import com.realmdemo.DBBean.Person;
import com.uphyca.stetho_realm.RealmInspectorModulesProvider;

import io.realm.Realm;
import io.realm.RealmAsyncTask;
import io.realm.RealmChangeListener;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getName();
    /**
     * realm 数据库基础配置
     *
     * @param savedInstanceState
     */

    Realm realm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initRealm();
    }

    private void initRealm() {
        Realm.init(this);
        Stetho.initialize(
                Stetho.newInitializerBuilder(this)
                        .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
                        .enableWebKitInspector(RealmInspectorModulesProvider.builder(this).build())
                        .build());
        // 默认配置
//        Realm realm = Realm.getDefaultInstance();
        // 自定义配置

        RealmConfiguration config =
                new RealmConfiguration.Builder()
                        // 文件名
                        .name("test.realm")
                        // 版本号
                        .schemaVersion(0)
                        .build();
        realm = Realm.getInstance(config);

    }

    public void test(View view) {

        RealmAsyncTask transaction = realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                for (int i = 0; i < 10; i++) {
                    Person user = new Person();
                    user.setName("" + i);
                    user.setAge("18");
                    user.setSex("男");
                    realm.insert(user);

                }

            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                //成功回调
                Log.i(TAG, "onSuccess: 成功");
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                //失败回调
                Log.i(TAG, "error:" + error.getMessage());

            }
        });


    }


    public void read(View view) {

        RealmAsyncTask transaction = realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<Person> userList = realm.where(Person.class).findAll();


                for (int i = 0; i < userList.size(); i++) {
                    Log.i(TAG, "run: " + "\n" + userList.get(i).getAge() + "\n"
                            + userList.get(i).getSex() + "\n"
                            + userList.get(i).getName() + "\n"
                    );
                }

            }
        });


    }


    public void del(View view) {

        RealmAsyncTask transaction = realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                final RealmResults<Person> userList = realm.where(Person.class).findAll();
                for (int i = 0; i <userList.size(); i++) {
                    userList.get(i).deleteFromRealm();
                }

            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        realm.close();
    }
}
